package com.alan.wifishare.view.impl.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.alan.libapi.model.business.UserSession;
import com.alan.library.allog.KLog;
import com.alan.wifishare.R;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.app.UserConfig;
import com.alan.wifishare.presenter.UserRegisterPS;
import com.alan.wifishare.view.BaseActivity;
import com.alan.wifishare.view.impl.ActivityJumpUtil;
import com.alan.wifishare.view.iview.IVUserRegister;

/**
 * @className: UserRegisterActivity
 * @describtion: UserRegisterActivity
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/20 17:08
 */
public class UserRegisterActivity extends BaseActivity implements IVUserRegister, View.OnClickListener {

    private static UserRegisterPS userRegisterPS = null;
    private Button userRegisterBackBtn;
    private EditText userRegisterNickNameEt;
    private EditText userRegisterPhoneEt;
    private EditText userRegisterVerifyCodeEt;
    private EditText userRegisterPwdEt;
    private Button userRegisterSendVcodeBtn;
    private CheckBox userRegisterProtocolCb;
    private TextView userRegisterProtocolTv;
    private Button userRegisterSubmitBtn;
    private Handler timerHandler = new Handler();
    private Runnable timerRunable;
    private boolean isCanSendVerifyCode = true;
    private int remainSecond = 60;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_register_layout);

        initView();
        userRegisterPS = new UserRegisterPS(this);
        isCanSendVerifyCode = true;
        timerRunable = new Runnable() {
            @Override
            public void run() {
                remainSecond--;
                KLog.e("remainSecond = " + remainSecond);
                if(remainSecond <= 0){
                    remainSecond = 60;
                    timerHandler.removeCallbacks(this);
                    userRegisterSendVcodeBtn.setText( "发送验证码");
                    isCanSendVerifyCode = true;
                }else {
                    userRegisterSendVcodeBtn.setText(remainSecond + "S后重发");
                    userRegisterSendVcodeBtn.postDelayed(timerRunable, 1000);
                }
            }
        };
    }

    @Override
    protected void initView() {
        //初始化控件，监听发送验证码的按钮及提交按钮，分别执行
        userRegisterBackBtn = (Button)findViewById(R.id.userRegisterBackBtn);
        userRegisterNickNameEt = (EditText)findViewById(R.id.userRegisterNickNameEt);
        userRegisterPhoneEt = (EditText)findViewById(R.id.userRegisterPhoneEt);
        userRegisterVerifyCodeEt = (EditText)findViewById(R.id.userRegisterVerifyCodeEt);
        userRegisterPwdEt = (EditText)findViewById(R.id.userRegisterPwdEt);
        userRegisterSendVcodeBtn = (Button)findViewById(R.id.userRegisterSendVcodeBtn);
        userRegisterProtocolCb = (CheckBox)findViewById(R.id.userRegisterProtocolCb);
        userRegisterProtocolTv = (TextView)findViewById(R.id.userRegisterProtocolTv);
        userRegisterSubmitBtn = (Button)findViewById(R.id.userRegisterSubmitBtn);

        userRegisterBackBtn.setOnClickListener(this);
        userRegisterSendVcodeBtn.setOnClickListener(this);
        userRegisterProtocolTv.setOnClickListener(this);
        userRegisterSubmitBtn.setOnClickListener(this);

    }

    @Override
    protected void onDestroy() {
        KLog.e();
        timerHandler.removeCallbacks(timerRunable);
        super.onDestroy();
    }

    @Override
    public String getUserNicName() {
        //从输入框获取用户昵称
        return userRegisterNickNameEt.getText().toString();
    }

    @Override
    public String getUserPhone() {
        //从输入框获取用户手机号
        return userRegisterPhoneEt.getText().toString();
    }

    @Override
    public String getVerifyCode() {
        //获取用户输入的验证码
        return userRegisterVerifyCodeEt.getText().toString();
    }

    @Override
    public String getUserPwd() {
        //获取用户密码
        return userRegisterPwdEt.getText().toString();
    }

    @Override
    public void toastMsg(String msg) {
        toastMsgShort(msg);
    }

    @Override
    public void sendVerifyCodeSuccess() {
        //验证码发送成功，验证码按钮开始60秒重新发送倒计时
        isCanSendVerifyCode = false;
        timerHandler.removeCallbacks(timerRunable);
        timerHandler.postDelayed(timerRunable, 1000);
    }

    @Override
    public void showProgress() {
        progressDialogShow();
    }

    @Override
    public void hideProgress() {
        progressDialogHiden();
    }

    @Override
    public void registerSuccess(UserSession userSession) {
        //注册成功后执行跳转
        toastMsg("注册成功");
        GlobalField.userConfig.setUserAccount(getUserPhone());
        GlobalField.userConfig.setUserPwd(getUserPwd());
        GlobalField.userSession = userSession;
        ActivityJumpUtil.jump2MainAct(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.userRegisterBackBtn:
                this.finish();
                break;
            case R.id.userRegisterSendVcodeBtn:
                KLog.e("isCanSendVerifyCode = " + isCanSendVerifyCode);
                if(isCanSendVerifyCode){
                    userRegisterPS.sendVerifyCode();
                }
                break;
            case R.id.userRegisterProtocolTv:
                toastMsg("查看用户使用协议");
                break;
            case R.id.userRegisterSubmitBtn:
                if(!userRegisterProtocolCb.isChecked()){
                    toastMsg("注册需阅读并同意用户使用协议");
                }else {
                    userRegisterPS.submitRegisterInfo();
                }
                break;
            default:
                break;
        }
    }
}
